﻿using HCQ2_Common;
using HCQ2_Model.SysModel;
using HCQ2_Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace HCQ2UI_Logic.AdminController
{
    /// <summary>
    ///  数据字典控制器
    /// </summary>
    public class SysItemCodeController: BaseLogic
    {
        #region 1.0 数据字典首次进入页面 +ActionResult List()
        /// <summary>
        ///  数据字典首次进入页面
        /// </summary>
        /// <returns></returns>
        [HCQ2_Common.Attributes.Element]
        [HCQ2_Common.Attributes.Load]
        [HttpGet]
        public ActionResult List()
        {
            return View();
        }
        #endregion

        #region 1.1 获取字典一栏数据 +ActionResult GetUserData()
        /// <summary>
        ///  获取字典一栏数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetDictData()
        {
            string keyword = RequestHelper.GetDeStrByName("keyword"),//代码
                isType = RequestHelper.GetDeStrByName("isType");
            int item_id = RequestHelper.GetIntByName("item_id");//id;
            List<DictListModel> list = operateContext.bllSession.T_ItemCode.GetItemTableList(keyword, item_id, isType);
            TableModel tModel = new TableModel()
            {
                total =list.Count,
                rows = list
            };
            return Json(tModel, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 1.2 获取字典结构树 数据 + ActionResult GetDocTreeData()
        /// <summary>
        ///  获取字典结构树 数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetDocTreeData()
        {
            return Json(operateContext.bllSession.T_ItemCode.GetItemList());
        }
        #endregion

        #region 1.3.1 添加根字典 + ActionResult AddNode(DictParamModel model)
        /// <summary>
        ///  添加字典
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddNode(DictParamModel model)
        {
            ModelState.Remove("item_id");
            if (!ModelState.IsValid)
                return operateContext.RedirectAjax(1, "数据验证失败~", getErrorMessage(ModelState), "");
            int mark,mainCount;
            mainCount = operateContext.bllSession.T_ItemCode.SelectCount(s => s.item_code.Equals(model.item_code));
            if (mainCount > 0)
                return operateContext.RedirectAjax(1, "数据验证失败~", "当前代码已被其他字典占用，请重新设置！", "");
            var item = new HCQ2_Model.T_ItemCode
            {
                item_name = model.item_name,
                item_code = model.item_code,
                item_note = model.item_note,
                enLang = model.enLang,
                langCode = Helper.GetGuid,
                whereValue = model.whereValue,
                numValue = model.numValue,
                creator_id = operateContext.Usr.user_id,
                creator_name = operateContext.Usr.user_name,
                creator_date = DateTime.Now
            };
            mark = operateContext.bllSession.T_ItemCode.Add(item);
            if (mark > 0)
            {
                model.item_id = item.item_id;
                return operateContext.RedirectAjax(0, "添加成功~", model, "");
            }
            return operateContext.RedirectAjax(1, "数据添加失败~", "", "");
        }
        #endregion

        #region 1.3.2 添加子字典 + ActionResult AddChildNode(DictChildParamModel model)
        /// <summary>
        ///  添加子字典
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddChildNode(DictChildParamModel model)
        {
            ModelState.Remove("itemId");
            ModelState.Remove("codeOrder");
            if (!ModelState.IsValid)
                return operateContext.RedirectAjax(1, "数据验证失败~", getErrorMessage(ModelState), "");
            int main_id = RequestHelper.GetIntByName("main_id"), mark;
            if (main_id <= 0)
                return operateContext.RedirectAjax(1, "数据验证失败~", "", "");
            if (model.codeOrder <= 0)
            {
                int? code_order = operateContext.bllSession.T_ItemCodeMenum.Select(s => s.item_id == main_id).OrderByDescending(s => s.code_order).FirstOrDefault()?.code_order;
                model.codeOrder = (code_order == null) ? 1 : ((int)code_order) + 1;
            }
            var item = new HCQ2_Model.T_ItemCodeMenum
            {
                item_id = main_id,
                if_system = 0,
                code_name = model.itemName,
                code_value = model.itemCode,
                code_note = model.itemNote,
                code_type = model.codeType,
                code_pid = model.codePid,
                if_child = model.if_child,
                code_order = model.codeOrder,
                langCode = Helper.GetGuid,
                numValue = model.numValue2,
                enLang = model.enLang2,
                create_id = operateContext.Usr.user_id,
                create_date = DateTime.Now
            };
            mark = operateContext.bllSession.T_ItemCodeMenum.Add(item);           
            if (mark > 0 && model.codePid > 0)
                operateContext.bllSession.T_ItemCodeMenum.Modify(new HCQ2_Model.T_ItemCodeMenum { if_child = 0 }, s => s.code_id == model.codePid, "if_child");
            if (mark > 0)
            {
                model.itemId = item.code_id;
                return operateContext.RedirectAjax(0, "添加成功~", model, "");
            }
            return operateContext.RedirectAjax(1, "数据添加失败~", "", "");
        }
        #endregion

        #region 1.4.1 编辑根字典 + ActionResult AddNode(DictParamModel model)
        /// <summary>
        ///  编辑根字典
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditNode(DictParamModel model)
        {
            ModelState.Remove("code_pid");
            if (!ModelState.IsValid)
                return operateContext.RedirectAjax(1, "数据验证失败~", getErrorMessage(ModelState), "");
            int mainCount;
            mainCount = operateContext.bllSession.T_ItemCode.SelectCount(s => s.item_id != model.item_id && s.item_code.Equals(model.item_code));
            if (mainCount > 0)
                return operateContext.RedirectAjax(1, "数据验证失败~", "编辑的代码已被其他字典占用，请重新设置！", "");
            var item = new HCQ2_Model.T_ItemCode
            {
                item_name = model.item_name,
                item_code = model.item_code,
                item_note = model.item_note,
                numValue = model.numValue,
                whereValue = model.whereValue,
                enLang = model.enLang,
                update_date = DateTime.Now
            };
            operateContext.bllSession.T_ItemCode.Modify(item, s => s.item_id == model.item_id, "item_name", "item_code", "enLang", "item_note", "whereValue", "numValue", "update_date");
            return operateContext.RedirectAjax(0, "编辑成功~", model, "");
        }
        #endregion

        #region 1.4.2 编辑子字典 + ActionResult EditChildNode(DictChildParamModel model)
        /// <summary>
        ///  编辑子字典
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditChildNode(DictChildParamModel model)
        {
            ModelState.Remove("code_pid");
            if (!ModelState.IsValid)
                return operateContext.RedirectAjax(1, "数据验证失败~", getErrorMessage(ModelState), "");
            int main_id = RequestHelper.GetIntByName("main_id");
            if (model.codeOrder <= 0)
            {
                int? code_order = operateContext.bllSession.T_ItemCodeMenum.Select(s => s.item_id == main_id).OrderByDescending(s => s.code_order).FirstOrDefault()?.code_order;
                model.codeOrder = (code_order == null) ? 1 : ((int)code_order) + 1;
            }
            var item = new HCQ2_Model.T_ItemCodeMenum
            {
                code_name = model.itemName,
                code_value = model.itemCode,
                code_note = model.itemNote,
                code_type = model.codeType,
                enLang = model.enLang2,
                numValue = model.numValue2,
                code_order = model.codeOrder,
                update_date = DateTime.Now
            };
            operateContext.bllSession.T_ItemCodeMenum.Modify(item, s => s.code_id == model.itemId, "code_name", "code_value", "code_note", "code_type", "enLang", "numValue", "code_order", "update_date");
            return operateContext.RedirectAjax(0, "编辑成功~", model, "");
        }
        #endregion

        #region 1.5 删除字典 +ActionResult DelDictById(int id)
        /// <summary>
        ///  删除字典
        /// </summary>
        /// <param name="id">用户id</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DelDictById(int id)
        {
            if (id <= 0)
                return operateContext.RedirectAjax(1, "需要删除的数据不存在~", "", "");
            string isType = RequestHelper.GetStrByName("isType");
            int mark;
            if (isType.Equals("itemCode"))
            {
                //主表
                int count = operateContext.bllSession.T_ItemCodeMenum.SelectCount(s => s.item_id == id);
                if (count > 0)
                    return operateContext.RedirectAjax(1, "字典下拉还有数据，请先删除下拉项~", "", "");
                //判断是否有图片附件
                var logoUrl = operateContext.bllSession.T_ItemCode.FindSingle(s => s.item_id == id)?.logoUrl;
                if (!string.IsNullOrEmpty(logoUrl) && System.IO.File.Exists(Server.MapPath(logoUrl)))
                    System.IO.File.Delete(Server.MapPath(logoUrl));
                mark = operateContext.bllSession.T_ItemCode.Delete(s => s.item_id == id);
            }
            else
            {
                //下拉表
                var logoUrl = operateContext.bllSession.T_ItemCodeMenum.FindSingle(s => s.code_id == id)?.logoUrl;
                if (!string.IsNullOrEmpty(logoUrl) && System.IO.File.Exists(Server.MapPath(logoUrl)))
                    System.IO.File.Delete(Server.MapPath(logoUrl));
                mark = operateContext.bllSession.T_ItemCodeMenum.Delete(s => s.code_id == id);
            }
            if (mark > 0)
                return operateContext.RedirectAjax(0, "数据删除成功~", "", "");
            return operateContext.RedirectAjax(1, "数据删除失败~", "", "");
        }
        #endregion
    }
}